package edu.jptest.serviceImp;

import java.util.ArrayList;
import java.util.List;

import edu.jptest.bean.ExaminationPaper;
import edu.jptest.bean.ExamineQuestion;
import edu.jptest.bean.ExamineQuestionSubject;
import edu.jptest.bean.ExamineQuestionType;
import edu.jptest.dao.ExaminationPaperDAO;
import edu.jptest.dao.ExamineQuestionDAO;
import edu.jptest.dao.ExamineQuestionSubjectDAO;
import edu.jptest.dao.ExamineQuestionTypeDAO;
import edu.jptest.dao.QuestionDAO;
import edu.jptest.dao.QuestionOriginDAO;
import edu.jptest.service.IExaminationPaperService;

public class ExaminationPaperService implements IExaminationPaperService {

	// 教师组卷
	public void makeExaminationPaperByTeacher(
			ExaminationPaper examinationPaper,
			ExamineQuestionSubject examineQuestionSubject,
			ExamineQuestionType examineQuestionType, int testPointId) {
		ExaminationPaperDAO examinationPaperDAO = new ExaminationPaperDAO();
		ExamineQuestionSubjectDAO examineQuestionSubjectDAO = new ExamineQuestionSubjectDAO();
		ExamineQuestionTypeDAO examineQuestionTypeDAO = new ExamineQuestionTypeDAO();
		QuestionOriginDAO questionOriginDAO = new QuestionOriginDAO();
		QuestionDAO questionDAO = new QuestionDAO();
		ExamineQuestionDAO examineQuestionDAO = new ExamineQuestionDAO();
		List<Integer> questionOriginIds;
		List<Integer> questionIds;
		int examinePaperId;// 此试卷的Id
		// 将该试卷的某些具体信息插入试卷表
		examinationPaperDAO.insertExaminationPaper(examinationPaper);
		// 通过组卷时间获得刚组试卷的Id
		examinePaperId = examinationPaperDAO
				.getExaminationPaperByTime(examinationPaper.getInsertTime());
		examineQuestionSubject.setExaminePaperId(examinePaperId);
		// 将该试卷中的题目插入卷题目表
		examineQuestionSubjectDAO
				.insertExamineQuestionSubject(examineQuestionSubject);
		examineQuestionType.setExaminePaperId(examinePaperId);
		// 将该试卷中的题型插入卷题型表
		examineQuestionTypeDAO.insertExamineQuestionType(examineQuestionType);
		// 通过试题范围获得试题来源
		if (examinationPaper.getLessonBegin() != null) {
			questionOriginIds = questionOriginDAO
					.getQuestionOriginIdByBookNameAndLessons(examinationPaper
							.getPaperOrigin(), examinationPaper
							.getLessonBegin(), examinationPaper.getLessonEnd());
		} else {
			questionOriginIds = questionOriginDAO
					.getQuestionOriginIdByBookNameAndUnits(examinationPaper
							.getPaperOrigin(), examinationPaper.getUnitBegin(),
							examinationPaper.getUnitEnd());
		}
		// 根据试题数、试题来源和试题题型或考点从题库中随机抽取试题
		int questionOrginId;
		int questionId;
		for (int i = 0; i < examineQuestionType.getQuestionTypeSum(); i++) {
			// 从来源id中随机获取某个来源id
			questionOrginId = questionOriginIds.get((int) (Math.random()
					* questionOriginIds.size() - 1));
			if (testPointId == 0)// 通过题型组卷，不包含考点
			{
				questionIds = questionDAO.getQuestionsIdByOrginAndType(
						questionOrginId, examineQuestionType
								.getQuestionTypeId());
				questionId = questionIds.get((int) (Math.random()
						* questionIds.size() - 1));
				// 如果是用户自行添加的题目则将题目添加到试题中的卷题目属性（同一道题在多个试卷中的题目如果不一样的话不行）
				if ((examineQuestionSubject.getSelfSubject() != null)
						|| !(examineQuestionSubject.equals("")))
					questionDAO.getQuestionByQuestionId(questionId)
							.setExamineQuestionSubjectId(
									examineQuestionSubject
											.getExamineQuestionSubjectId());
				// 卷体表中插入刚选好的试题
				ExamineQuestion examineQuestion = new ExamineQuestion();
				examineQuestion.setExaminePaperId(examinePaperId);
				examineQuestion.setQuestionId(questionId);
				examineQuestion.setQuestionInPapernum(i);
				examineQuestionDAO.insertExamineQuestion(examineQuestion);
			} else // 通过题型、考点组卷
			{
				questionIds = questionDAO
						.getQuestionsIdByOriginAndTypeAndTestPoint(
								questionOrginId, examineQuestionType
										.getQuestionTypeId(), testPointId);
				questionId = questionIds.get((int) (Math.random()
						* questionIds.size() - 1));
				ExamineQuestion examineQuestion = new ExamineQuestion();
				examineQuestion.setExaminePaperId(examinePaperId);
				examineQuestion.setQuestionId(questionId);
				examineQuestion.setQuestionInPapernum(i);
				examineQuestionDAO.insertExamineQuestion(examineQuestion);
			}
		}
	}

	// 用户自行组卷,用户自行组的试卷系统不保存。
	public List<Integer> makeExaminationPaperByUser(
			ExaminationPaper examinationPaper,
			ExamineQuestionType examineQuestionType, int testPointId) {
		// 组卷集合
		List<Integer> questionsId = new ArrayList<Integer>();
		List<Integer> questionOriginIds;
		List<Integer> questionIds;
		QuestionOriginDAO questionOriginDAO = new QuestionOriginDAO();
		QuestionDAO questionDAO = new QuestionDAO();
		// 通过试题范围获得试题来源
		if (examinationPaper.getLessonBegin() != null) {
			questionOriginIds = questionOriginDAO
					.getQuestionOriginIdByBookNameAndLessons(examinationPaper
							.getPaperOrigin(), examinationPaper
							.getLessonBegin(), examinationPaper.getLessonEnd());
		} else {
			questionOriginIds = questionOriginDAO
					.getQuestionOriginIdByBookNameAndUnits(examinationPaper
							.getPaperOrigin(), examinationPaper.getUnitBegin(),
							examinationPaper.getUnitEnd());
		}
		// 根据试题数、试题来源和试题题型或考点从题库中随机抽取试题
		int questionOrginId;
		int questionId;
		for (int i = 0; i < examineQuestionType.getQuestionTypeSum(); i++) {
			// 从来源id中随机获取某个来源id
			questionOrginId = questionOriginIds.get((int) (Math.random()
					* questionOriginIds.size() - 1));
			if (testPointId == 0)// 通过题型组卷，不包含考点
			{
				questionIds = questionDAO.getQuestionsIdByOrginAndType(
						questionOrginId, examineQuestionType
								.getQuestionTypeId());
				questionId = questionIds.get((int) (Math.random()
						* questionIds.size() - 1));
				questionsId.add(questionId);
			} else // 通过题型、考点组卷
			{
				questionIds = questionDAO
						.getQuestionsIdByOriginAndTypeAndTestPoint(
								questionOrginId, examineQuestionType
										.getQuestionTypeId(), testPointId);
				questionId = questionIds.get((int) (Math.random()
						* questionIds.size() - 1));
				// 将根据条件随机抽取的试题加入到组卷集合中
				questionsId.add(questionId);
			}
		}
		return questionsId;
	}

	// 更改试卷
	public boolean updateExaminationPaper(int examinePaper_id) {
		ExaminationPaperDAO examinationPaperDAO = new ExaminationPaperDAO();
		return examinationPaperDAO.updateExaminationPaper(examinationPaperDAO
				.getExaminationPaperByExaminationPaperId(examinePaper_id));
	}

	// 删除组卷
	public void deleteMakedExaminePaper(int examinePaper_id,
			int examineQuestionType_id, int examineQuestionSubject_id,
			int examineQuestion_id) {
		ExaminationPaperDAO examinationPaperDAO = new ExaminationPaperDAO();
		ExamineQuestionSubjectDAO examineQuestionSubjectDAO = new ExamineQuestionSubjectDAO();
		ExamineQuestionTypeDAO examineQuestionTypeDAO = new ExamineQuestionTypeDAO();
		ExamineQuestionDAO examineQuestionDAO = new ExamineQuestionDAO();
		QuestionDAO questionDAO = new QuestionDAO();
		examinationPaperDAO.deleteExaminationPaperr(examinePaper_id);
		examineQuestionSubjectDAO
				.deleteExamineQuestionSubject(examineQuestionSubject_id);
		examineQuestionTypeDAO
				.deleteExamineQuestionType(examineQuestionType_id);
		List<Integer> examineQuestionIds = examineQuestionDAO
				.getExamineQuestionsIdByExaminationPaperId(examinePaper_id);
		int questionId;
		for (int i = 0; i < examineQuestionIds.size(); i++) {
			questionId = examineQuestionDAO
					.getExamineQuestionByExamineQuestionId(
							examineQuestionIds.get(i)).getQuestionId();
			// 判断已组试卷中所要删除的大题中的小题题目是否与所要删除的题目相等，相等则为要删除的试题。
			if (examineQuestionSubject_id == questionDAO
					.getQuestionByQuestionId(questionId).getQuestionSubjectId())
				examineQuestionDAO.deleteExamineQuestion(examineQuestionIds
						.get(i));
		}
	}
	
          //*****************用户答卷时分三种试卷（教材中、教师组好、自行组卷）***********************//
	
	// 获得教材中固有的试卷（每课一练、单元测试等）
	public List<List<Integer>> getExaminePaperInBook(int questionOrigin_id) {
		QuestionDAO questionDAO = new QuestionDAO();
		// 集合中放入的是每个题目下对应的试题集合
		List<List<Integer>> examinePaper = new ArrayList<List<Integer>>();
		List<Integer> questions;
		int count = 1;
		do {
			// 获得相应题目序号下的试题集合
			questions = questionDAO.getQuestionsIdByOriginIdAndSubject(
					questionOrigin_id, count);
			// 将此题目下获得的试题集合添加到试卷集合中
			examinePaper.add(questions);
			count++;
		} while (questionDAO.getQuestionsIdByOriginIdAndSubject(
				questionOrigin_id, count) != null);
		return examinePaper;
	}  

	// 获得教师组好的试卷
	public List<List<Integer>> getTeaExaminePaper(String paperName) {
		List<List<Integer>> examinePaper = new ArrayList<List<Integer>>();
		ExaminationPaperDAO examinationPaperDAO = new ExaminationPaperDAO();
		ExamineQuestionSubjectDAO examineQuestionSubjectDAO = new ExamineQuestionSubjectDAO();
		QuestionDAO questionDAO = new QuestionDAO();
		int examinePaperId = examinationPaperDAO
				.getExaminationPaperByName(paperName);
		int count = examineQuestionSubjectDAO
				.getExamineQuestionSubjectIdByExaminePaperId(examinePaperId)
				.size();
		for (int i = 1; i <= count; i++) {
			// 通过试卷id及题目序号获得相应卷题目id,通过该id获得相应卷题目对象
			ExamineQuestionSubject examineQuestionSubject = examineQuestionSubjectDAO
					.getExamineQuestionSubjectByExamineQuestionSubjectId(examineQuestionSubjectDAO
							.getExamineQuestionSubjectIdByNumAndE_P_Id(
									examinePaperId, i));
			// 两种情况，一种自己添加的，另一种现有的
			if (examineQuestionSubject.getQuestionSubjectId() != null) {
				// 将给试卷中的相应题目下的试题集合加入试卷中
				examinePaper.add(questionDAO
						.getQuestionsIdBySubjectIdAndExamPaperId(
								examineQuestionSubject.getQuestionSubjectId(),
								examinePaperId));
			} else
				examinePaper.add(questionDAO
						.getQuestionsIdByExaminPaperSubjectIdAndExamPaper(
								examineQuestionSubject
										.getExamineQuestionSubjectId(),
								examinePaperId));
		}
		return examinePaper;
	}

}
